//
//  FYEmoticonKeyBoard.swift
//  Mircore
//
//  Created by apple on 16/8/17.
//  Copyright © 2016年 飞羽. All rights reserved.
//

import UIKit

//表情键盘自定义视图

/**
 *    表情视图   UICollectionView
 
 toolbar视图  UIStackView
 */

//重用标记
private  let  EmoticonCollectionViewCellIdentifier  = "EmoticonCollectionViewCellIdentifier"

class FYEmoticonKeyBoard: UIView {
    
    //MARK:-  懒加载控件
    
    private  lazy  var  toolBar: FYEmoticonToolBar = {
        
        let  toolBar =   FYEmoticonToolBar(frame: CGRectZero)
        
        return toolBar
        
    }()
    
    
    //表情视图
    
    private  lazy  var  emoticonCollectionView: UICollectionView = {
        
        
        let  flowLayout  = UICollectionViewFlowLayout()
        
        
        //设置水平方向滚动
        
        flowLayout.scrollDirection = .Horizontal
        
        let  view = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        
        view.backgroundColor  = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        //设置水平和垂直方向的滚动指示器不显示
        
        view.showsHorizontalScrollIndicator =  false
        
        view.showsVerticalScrollIndicator = false
        
        //开启分页
        
        view.pagingEnabled = true
        
        //不开启弹簧
        
        view.bounces  = false
        
        //注册cell
        
        view.registerClass(FYEmoticonCollectionViewCell.self, forCellWithReuseIdentifier: EmoticonCollectionViewCellIdentifier)
        
        //设置代理
        
        view.delegate = self
        
        //数据源代理
        
        view.dataSource = self
        
        return view
    }()
    
    //页数指示器
    
    private  lazy  var  pageControl: UIPageControl  = {
        
        
        let  pageCtr = UIPageControl()
        
        pageCtr.currentPageIndicatorTintColor  = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_selected")!)
        
        pageCtr.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
        
        return pageCtr
    }()
    
    
    override  init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required  init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder: ) has not been implemented")
    }
    
    
    //添加控件
    
    private  func  setupUI() {
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        
        //默认页数数据绑定
        
        let  defaultIndexPath: NSIndexPath = NSIndexPath(forItem: 0, inSection: 0)
        
        //设置数据
        
        setPageControlDataForIndexPath(defaultIndexPath)
        
        //添加页数指示控件
        addSubview(emoticonCollectionView)
        addSubview(pageControl)
        
        
        //添加控件
        
        addSubview(toolBar)
        
        //设置约束
        
        emoticonCollectionView.snp_makeConstraints { (make) in
            
            make.top.equalTo(self)
            
            make.leading.equalTo(self)
            
            make.trailing.equalTo(self)
            
            make.bottom.equalTo(toolBar.snp_top)
            
        }
        
        pageControl.snp_makeConstraints { (make) in
            
            make.bottom.equalTo(emoticonCollectionView)
            
            make.centerX.equalTo(emoticonCollectionView)
            
            make.height.equalTo(10)
        }
        
        
        toolBar.snp_makeConstraints { (make) in
            
            make.bottom.equalTo(self)
            
            make.leading.equalTo(self)
            
            make.trailing.equalTo(self)
            
            make.height.equalTo(35)
        }
        
        toolBar.didSeletedButtonClosure = { [weak self] (type: FYEmoticonToolBarButtonType) in
            
            //滚动到的indexPath
            
            let  indexPath: NSIndexPath
            
            switch type{
                
            case .Normal:
                
                print("默认")
                
                indexPath =  NSIndexPath(forItem: 0, inSection: 0)
            case .Emoji:
                
                indexPath  = NSIndexPath(forItem: 0, inSection: 1)
                print("Emoji")
                
            case .LXH:
                
                indexPath = NSIndexPath(forItem: 0, inSection: 2)
                print("LXH")
            }
            
            //滚动到指定位置  不需要开启动画
            
            self?.emoticonCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            
            
            self?.setPageControlDataForIndexPath(indexPath)
        }
    }
    
    //通过indexpath绑定页数控件的数据
    
    private  func  setPageControlDataForIndexPath(indexPath: NSIndexPath) {
        
        pageControl.numberOfPages = FYEmoticonTools.sharedTools.allEmoticonArray[indexPath.section].count
        
        pageControl.currentPage = indexPath.item
        
    }
    
    //设置子控件布局方式
    
    override  func  layoutSubviews() {
        
        
        super.layoutSubviews()
        
        let  flowLayout = emoticonCollectionView.collectionViewLayout  as! UICollectionViewFlowLayout
        
        //设置每项的大小
        
        flowLayout.itemSize  = emoticonCollectionView.size
        
        //设置水平间距和垂直间距
        
        flowLayout.minimumInteritemSpacing  = 0
        
        flowLayout.minimumLineSpacing  = 0
    }
    
    
    
}


extension  FYEmoticonKeyBoard: UICollectionViewDataSource,UICollectionViewDelegate  {
    
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        
        return  FYEmoticonTools.sharedTools.allEmoticonArray.count
    }
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return FYEmoticonTools.sharedTools.allEmoticonArray[section].count
    }
    
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        
        let  cell  = collectionView.dequeueReusableCellWithReuseIdentifier(EmoticonCollectionViewCellIdentifier, forIndexPath: indexPath) as! FYEmoticonCollectionViewCell
        
        //设置绑定数据
        
        cell.emoticons  = FYEmoticonTools.sharedTools.allEmoticonArray[indexPath.section][indexPath.item]
        
        return cell
    }
    
    //监听collectionView滚动
    
    func  scrollViewDidScroll(scrollView: UIScrollView) {
        
        //获取当前屏幕显示的cell 获取的cell不会对其进行x坐标排序  随机返回的
        
        let  cells  = emoticonCollectionView.visibleCells().sort { (firstCell, secondCell) -> Bool in
            
            return  firstCell.x < secondCell.x
        }
        
        
        //判断哪个cell显示多
        
        if  cells.count == 2 {
            
            //获取第一个cell
            
            let  firstCell = cells.first!
            
            //获取第二个cell
            
            let  secondCell  = cells.last!
            
            
            //第一个偏移量差值
            
            let firstContentOffSetX = abs(firstCell.x  - scrollView.contentOffset.x)
            
            //第二个偏移量差值
            
            let  secondContentOffSetX = secondCell.x - scrollView.contentOffset.x
            
            
            //记录哪个cell显示的多
            
            let  cell: UICollectionViewCell
            
            
            if  firstContentOffSetX < secondContentOffSetX  {
                
                cell  = firstCell
                
            }else  {
                
                cell  = secondCell
            }
            
            //根据cell获取相应的indexPath
            
            let  indexPath  = emoticonCollectionView.indexPathForCell(cell)!
            
            //获取indexPath对应的是哪一个组
            
            let  section  = indexPath.section
            
            //选中指定这组数据
            
            toolBar.selectedButtonWithSection(section)
            
            setPageControlDataForIndexPath(indexPath)
        }
        
    }
    
    
}

